package com.phpxiu.app.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;

import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.MySelfInfo;
import com.tencent.TIMManager;

/**
 * 欢迎界面
 * Created by WuJinZhou on 2016/4/26.
 */
public class WelCome extends UIBase implements View.OnClickListener{
    protected final static String TAG = "WelCome";
    private final static int WELCOME_FINISH = 0x1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isGotoLogin()) {//未登录或登录无效
            gotoLogin();
            return;
        }
        if (!isTaskRoot()) {
            finish();
            return;
        }
        setContentView(R.layout.welcome);
        handler.sendEmptyMessageDelayed(WELCOME_FINISH, 3000);//默认播放3秒图片动画后再检测是否可以跳转
        findViewById(R.id.welcome_image).setOnClickListener(this);
    }

    /**
     * 判断登录是否失效
     *
     * @return true 代表需要重新登录
     */
    public boolean isGotoLogin() {
        if (MySelfInfo.getInstance().getId() == null || TIMManager.getInstance().getLoginUser() == null) {
            return true;//有账号不需要登录
        }
        return false;
    }

    /**
     * 前往主界面
     */
    private void goHome() {
        Intent main = new Intent(this, Main.class);
        startActivity(main);
        finish();
    }

    /**
     * 前往登录
     */
    private void gotoLogin() {
        Intent login = new Intent(this, Login.class);
        startActivity(login);
        finish();
    }

    public void handleMsg(Message msg) {
        //欢迎完毕
        switch (msg.what) {
            case WELCOME_FINISH:
                goHome();
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        handler.removeCallbacksAndMessages(null);//终止欢迎
        goHome();//前往主页
    }

    @Override
    public void finish() {
        handler.removeCallbacksAndMessages(null);
        super.finish();
    }

}
